package com.tiancheng.trade.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tiancheng.trade.order.bo.CreditOrderPayQueryBo;
import com.tiancheng.trade.order.mapper.CreditOrderPayRecordMapper;
import com.tiancheng.trade.order.model.CreditOrderPayRecord;
import com.tiancheng.trade.order.service.CreditOrderPayRecordService;
import com.tiancheng.trade.order.util.CommonUtil;
import com.tiancheng.trade.commom.core.utils.DateTimeUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 类 描 述：信用订单支付记录
 * 创建时间：2024-09-16 13:49
 * 创 建 人：likailun
 */
@Slf4j
@Service
public class CreditOrderPayRecordServiceImpl implements CreditOrderPayRecordService {
    @Resource
    private CreditOrderPayRecordMapper creditOrderPayRecordMapper;
    @Override
    public void create(CreditOrderPayQueryBo co,String orderId,String creditType) {
        List<CreditOrderPayRecord> cos = creditOrderPayRecordMapper.selectList(new LambdaQueryWrapper<CreditOrderPayRecord>()
                .eq(CreditOrderPayRecord::getAlipayTradeNo, co.getTradeNo()));
        CreditOrderPayRecord orderPayRecord=null;
        if(CollectionUtils.isEmpty(cos)){
            orderPayRecord=new CreditOrderPayRecord();
        }else{
            orderPayRecord=cos.get(0);
        }
        orderPayRecord.setOrderId(orderId);
        orderPayRecord.setTradeStatus(co.getTradeStatus());
        orderPayRecord.setAlipayBuyerLogonId(co.getBuyerLogonId());
        orderPayRecord.setAlipayTradeNo(co.getTradeNo());
        orderPayRecord.setAlipayBuyerUserId(co.getBuyerUserId());
        orderPayRecord.setTotalAmount(CommonUtil.long2Integer(co.getTotalAmount()));
        orderPayRecord.setSettleAmount(CommonUtil.long2Integer(co.getSettleAmount()));
        orderPayRecord.setPayAmount(CommonUtil.long2Integer(co.getPayAmount()));
        orderPayRecord.setBuyerPayAmount(CommonUtil.long2Integer(co.getBuyerPayAmount()));
        orderPayRecord.setPointAmount(CommonUtil.long2Integer(co.getPointAmount()));
        orderPayRecord.setInvoiceAmount(CommonUtil.long2Integer(co.getInvoiceAmount()));
        orderPayRecord.setSendPayDate(DateTimeUtil.timestamp2Datetime(co.getSendPayDate()));
        orderPayRecord.setReceiptAmount(CommonUtil.long2Integer(co.getReceiptAmount()));
        orderPayRecord.setChargeAmount(CommonUtil.long2Integer(co.getChargeAmount()));
        orderPayRecord.setMdiscountAmount(CommonUtil.long2Integer(co.getMdiscountAmount()));
        orderPayRecord.setDiscountAmount(CommonUtil.long2Integer(co.getDiscountAmount()));
        orderPayRecord.setGwOrderId(co.getGwOrderId());
        orderPayRecord.setCreditType(creditType);
        if(orderPayRecord.getCreatedDt()==null){
            orderPayRecord.setCreatedDt(LocalDateTime.now());
        }
        if(orderPayRecord.getId()!=null){
            orderPayRecord.setLastUpdDt(LocalDateTime.now());
            creditOrderPayRecordMapper.update(orderPayRecord,new LambdaQueryWrapper<CreditOrderPayRecord>()
                    .eq(CreditOrderPayRecord::getId,orderPayRecord.getId()));
            return;
        }
        creditOrderPayRecordMapper.insert(orderPayRecord);
    }

    @Override
    public CreditOrderPayRecord get(String gwOrderId) {
        List<CreditOrderPayRecord> cos = creditOrderPayRecordMapper.selectList(new LambdaQueryWrapper<CreditOrderPayRecord>()
                .eq(CreditOrderPayRecord::getGwOrderId, gwOrderId));
        return CollectionUtils.isEmpty(cos)?null:cos.get(0);
    }
}
